package soulkun.library.utils.screen

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.Rect
import android.os.Build
import android.util.DisplayMetrics
import android.view.View
import android.view.WindowManager
import soulkun.library.LibraryInit
import soulkun.library.utils.R

object ScreenUtils {

    fun setTranslucentStatusBar(activity: Activity) {
        val window = activity.window
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = Color.TRANSPARENT
            fillInStatusBar(activity)
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
    }

    fun setTranslucentNavBar(activity: Activity) {
        val window = activity.window
        window.navigationBarColor = Color.TRANSPARENT
        fillInNavBar(activity)
    }

    fun setBlackStatusText(activity: Activity) {
        val decorView = activity.window.decorView
        var option = decorView.systemUiVisibility
        option = option or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        if (decorView.systemUiVisibility != option) {
            decorView.systemUiVisibility = option
        }
    }

    fun setWhiteStatusText(activity: Activity) {
        val decorView = activity.window.decorView
        var option = decorView.systemUiVisibility
        option = option and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
        if (decorView.systemUiVisibility != option) {
            decorView.systemUiVisibility = option
        }
    }

    fun setImmersive(activity: Activity) {
        val decorView = activity.window.decorView
        decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
    }

    fun clearImmersive(activity: Activity, status: Boolean, nav: Boolean) {
        val decorView = activity.window.decorView
        decorView.systemUiVisibility = 0
        if (status) {
            fillInStatusBar(activity)
        }
        if (nav) {
            fillInNavBar(activity)
        }
    }

    fun setViewFitSystemWindows(view: View) {
        val haveSetOffset = view.getTag(R.id.tag_900)
        if (haveSetOffset != null && haveSetOffset as Boolean) return
        view.setPadding(
            view.paddingLeft,
            view.paddingTop + getStatusBarHeight(),
            view.paddingRight,
            view.paddingBottom
        )
        view.setTag(R.id.tag_900, true)
    }

    fun fillInStatusBar(activity: Activity) {
        val decorView = activity.window.decorView
        var option = decorView.systemUiVisibility
        option =
            option or (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
        if (decorView.systemUiVisibility != option) {
            decorView.systemUiVisibility = option
        }
    }

    fun fillInNavBar(activity: Activity) {
        val decorView = activity.window.decorView
        if (decorView != null) {
            var option = decorView.systemUiVisibility
            option =
                option or (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
            if (decorView.systemUiVisibility != option) {
                decorView.systemUiVisibility = option
            }
        }
    }

    fun getStatusBarHeight(): Int {
        val res = LibraryInit.context.resources
        val resourceId = res.getIdentifier("status_bar_height", "dimen", "android")
        return if (resourceId != 0) {
            res.getDimensionPixelSize(resourceId)
        } else {
            DensityUtils.dp2px(20f)
        }
    }

    fun getNavBarHeight(): Int {
        val res = LibraryInit.context.resources
        val resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android")
        return if (resourceId != 0) {
            res.getDimensionPixelSize(resourceId)
        } else {
            0
        }
    }

    /**
     * 获得屏幕高度
     *
     * @param context
     * @return
     */
    fun getScreenWidth(context: Context): Int {
        val wm = context
                .getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val outMetrics = DisplayMetrics()
        wm.defaultDisplay.getMetrics(outMetrics)
        return outMetrics.widthPixels
    }

    /**
     * 获得屏幕宽度
     *
     * @param context
     * @return
     */
    fun getScreenHeight(context: Context): Int {
        val wm = context
                .getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val outMetrics = DisplayMetrics()
        wm.defaultDisplay.getMetrics(outMetrics)
        return outMetrics.heightPixels
    }

    /**
     * 获得状态栏的高度
     *
     * @param context
     * @return
     */
    fun getStatusHeight(context: Context): Int {
        var statusHeight = -1
        try {
            val clazz = Class.forName("com.android.internal.R\$dimen")
            val `object` = clazz.newInstance()
            val height = clazz.getField("status_bar_height")[`object`].toString().toInt()
            statusHeight = context.resources.getDimensionPixelSize(height)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return statusHeight
    }

    /**
     * 获取当前屏幕截图，包含状态栏
     *
     * @param activity
     * @return
     */
    fun snapShotWithStatusBar(activity: Activity): Bitmap? {
        val view = activity.window.decorView
        view.isDrawingCacheEnabled = true
        view.buildDrawingCache()
        val bmp = view.drawingCache
        val width = getScreenWidth(activity)
        val height = getScreenHeight(activity)
        var bp: Bitmap? = null
        bp = Bitmap.createBitmap(bmp, 0, 0, width, height)
        view.destroyDrawingCache()
        return bp
    }

    /**
     * 获取当前屏幕截图，不包含状态栏
     *
     * @param activity
     * @return
     */
    fun snapShotWithoutStatusBar(activity: Activity): Bitmap? {
        val view = activity.window.decorView
        view.isDrawingCacheEnabled = true
        view.buildDrawingCache()
        val bmp = view.drawingCache
        val frame = Rect()
        activity.window.decorView.getWindowVisibleDisplayFrame(frame)
        val statusBarHeight = frame.top
        val width = getScreenWidth(activity)
        val height = getScreenHeight(activity)
        var bp: Bitmap? = null
        bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
                - statusBarHeight)
        view.destroyDrawingCache()
        return bp
    }

}